package com.travelshare.business.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONArray;
import com.travelshare.business.domain.InfoSensitive;
import com.travelshare.business.domain.cache.CacheDataModel;
import com.travelshare.common.constant.CacheConstants;
import com.travelshare.common.core.redis.RedisCache;
import com.travelshare.common.utils.CommonUtils;
import com.travelshare.common.utils.StringUtils;
import com.travelshare.common.utils.spring.SpringUtils;

/**
 * 自定义缓存
 * */
public class CustomCacheUtils {

	/**
     * 分隔符
     */
    public static final String SEPARATOR = ",";
    
    /**
     * 设置缓存信息
     * 
     * @param cache 缓存信息
     */
    public static void setDataCache(CacheDataModel cache)
    {
    	List<CacheDataModel> caches = new ArrayList<>();
    	caches.add(cache);
        SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(cache.getKey()), caches);
    }
    
    /**
     * 缓存map数据
     * 
     * */
    public static void cacheDataMap(String key, Map<String, ?> map) {
    	 SpringUtils.getBean(RedisCache.class).setCacheMap(getCacheKey(key), map);
    }
    
    /**
     * 缓存map数据
     * 
     * */
    public static void incrementCacheMapValue(String key, String hKey, Integer value) {
    	 SpringUtils.getBean(RedisCache.class).incrementCacheMapValue(getCacheKey(key), hKey, value);
    }
    
    /**
     * 获取map缓存数据
     * 
     * */
    public static Map<String, ?> getCacheDataMap(String key) {
    	return SpringUtils.getBean(RedisCache.class).getCacheMap(getCacheKey(key));
    }
    
    /**
     * 获取map缓存数据
     * 
     * */
    public static Object  getCacheDataMapValue(String key, String id) {
    	Map<String, ?> map = SpringUtils.getBean(RedisCache.class).getCacheMap(getCacheKey(key));
    	return map.get(id);
    }
    
    /**
     * 获取缓存信息
     * 
     * @param key 参数键
     * @return  缓存信息
     * */
    public static CacheDataModel getCacheDataCache(String key) {
    	JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
    	if (StringUtils.isNotNull(arrayCache))
        {
    		List<CacheDataModel> list = arrayCache.toList(CacheDataModel.class);
            if(list.isEmpty()) {
            	return null;
            }
    		return list.get(0);
        }
        return null;
    }

    /**
     * 获取敏感词缓存
     * 
     * @param key 参数键
     * @return Map<Character, Map> 敏感词map
     */
    @SuppressWarnings("rawtypes")
	public static Map<Character, Map> getSensitiveCache()
    {
    	CacheDataModel data =  getCacheDataCache("tl_sensitive_words");
        if (StringUtils.isNotNull(data))
        {
        	List<InfoSensitive> list = data.getSensitives();
        	List<String> ls = new ArrayList<>();
        	for(InfoSensitive s : list) {
        		ls.add(s.getSensitiveWords());
        	}
            return CommonUtils.saveSensitiveWords(ls);
        }
        return null;
    }

    /**
     * 删除指定字典缓存
     * 
     * @param key 字典键
     */
    public static void removeCustomCache(String key)
    {
        SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(key));
    }

    /**
     * 清空字典缓存
     */
    public static void clearCustomCache()
    {
        Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(CacheConstants.CUSTOM_KEY + "*");
        SpringUtils.getBean(RedisCache.class).deleteObject(keys);
    }

    /**
     * 设置cache key
     * 
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey)
    {
        return CacheConstants.CUSTOM_KEY + configKey;
    }
}
